#!/bin/bash
root_dir="/usr/local/edata"
if [ ! -d "$root_dir" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir undetected"
  mkdir -p $root_dir
  if [ $? -eq 0 ];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") INFO creating $root_dir success"
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR creating $root_dir fail"
    exit 1
  fi
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir detected"
fi

jdk_ver=$(java -version 2>&1)
if [[ $jdk_ver == *"1.8.0_221"* ]];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO jdk 1.8.0_221 detected"
else
  if [[ $jdk_ver == *"not found"* ]];then
    echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING jdk is not found,installing..."
  else
    echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR other version of jdk detected (1.8.0_221 is required),uninstall it first"
    exit 1
  fi
fi

if [ ! -d "$root_dir/jdk" ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO $root_dir/jdk undetected"
  tar -xvf jdk-8u221-linux-x64.tar.gz
  mv jdk1.8.0_221 $root_dir/jdk
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") WARNING $root_dir/jdk detected"
fi

if [[ -z "$JAVA_HOME" ]];then
  echo "export JAVA_HOME=$root_dir/jdk" >> ~/.bashrc
  echo 'export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH' >> ~/.bashrc
  echo 'export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' >> ~/.bashrc
  echo "export JAVA_HOME=$root_dir/jdk" >> /etc/profile
  echo 'export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH' >> /etc/profile
  echo 'export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' >> /etc/profile
  source ~/.bashrc
  source /etc/profile
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO JAVA_HOME is set"
fi
java -version
if [ $? -eq 0 ];then
  echo "$(date +"%Y-%m-%d %H:%M:%S") INFO installing JDK success"
else
  echo "$(date +"%Y-%m-%d %H:%M:%S") ERROR installing JDK fail"
fi



